/*
 * =====================================================================================
 *
 *       Filename:  glassbutton.cc
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  12/02/11 18:40:36
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  H_Ley (), kalxd@126.com
 *        Company:  
 *
 * =====================================================================================
 */

#include "glassbutton.h"

GlassButton::GlassButton(QWidget *parent):QPushButton(parent){
	is_press=false;
	setStyleSheet("QPushButton{border-radius:2px;border:1px solid blue;}");
	InitButton();
}

GlassButton::~GlassButton(){
	delete normal_pix;
	delete hover_pix;
	delete focus_pix;
}

void GlassButton::InitButton(){
	int window_width=this->width();
	int window_height=this->height();
	normal_pix=new QPixmap;
	normal_pix->load("images/glassbutton_normal.png");
	*normal_pix=normal_pix->scaled(window_width,window_height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);

	hover_pix=new QPixmap;
	hover_pix->load("images/glassbutton_hover.png");
	*hover_pix=hover_pix->scaled(window_width,window_height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);

	focus_pix=new QPixmap;
	focus_pix->load("images/glassbutton_focus.png");
	*focus_pix=focus_pix->scaled(window_width,window_height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);

	setIcon(*normal_pix);
	setText(tr(""));
}

void GlassButton::enterEvent(QEvent *event){
	if(!hover_pix->isNull())
		setIcon(QIcon(*hover_pix));
}

void GlassButton::leaveEvent(QEvent *event){
	if(!normal_pix->isNull())
		setIcon(QIcon(*normal_pix));
}

void GlassButton::mousePressEvent(QMouseEvent *event){
	QPushButton::mousePressEvent(event);
	is_press=(event->buttons()==Qt::LeftButton);
	if(!focus_pix->isNull())
		setIcon(QIcon(*focus_pix));
}

void GlassButton::mouseReleaseEvent(QMouseEvent *event){
	QPushButton::mouseReleaseEvent(event);
	if(is_press)
		is_press=false;
	if(!normal_pix->isNull())
		setIcon(QIcon(*normal_pix));
}

void GlassButton::resizeEvent(QResizeEvent *event){
	int window_width=this->width();
	int window_height=this->height();
	*normal_pix=QPixmap(window_width,window_height);
	*hover_pix=QPixmap(window_width,window_height);
	*focus_pix=QPixmap(window_width,window_height);
}

void GlassButton::paintEvent(QPaintEvent *event){
	QPushButton::paintEvent(event);

	int width=this->width();
	int height=this->height();
	QPainter painter(this);
	painter.drawPixmap(0,0,width,height,*normal_pix);
	painter.end();
}
